﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Generic_Genetic_Algorithm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String[] ElementsString;
            float[] Limits = new float[2];
            int PopSize = Convert.ToInt32(textPopSize.Text);
            int GeneSize = Convert.ToInt32(textGeneSize.Text);
            listBox1.Items.Clear();

            Generic_Genetic_Algorithm_Class.GenericGeneticAlgorithm GenAlgo = null;

            if (radioText.Checked)
            {
                ElementsString = textBox3.Lines.ToList<String>().ToArray();
                GenAlgo = new Generic_Genetic_Algorithm_Class.GenericGeneticAlgorithm(0.4f, PopSize, GeneSize, ElementsString);
            }
            else if (radioFloat.Checked)
            {
                Limits[0] = (float) Convert.ToDouble(textFMin.Text);
                Limits[1] = (float) Convert.ToDouble(textFMax.Text);
                GenAlgo = new Generic_Genetic_Algorithm_Class.GenericGeneticAlgorithm(0.4f, PopSize, GeneSize, Limits);
            }

            for (int i = 0; i < PopSize; i++)
            {
                String Line = "";

                for (int j = 0; j < GeneSize; j++)
                {
                    if (Line.Length != 0) Line += ", ";

                    if (radioText.Checked)
                    {
                        Line = Line + GenAlgo.Population[i].StringGene[j];
                    }
                    else if (radioFloat.Checked)
                    {
                        Line = Line + GenAlgo.Population[i].FloatGene[j].ToString();
                    }
                }

                listBox1.Items.Add(Line);
            }

            System.Random RanGen = new Random();
            float[] eval = new float[PopSize];

            //Loop to generate random evaluation functions
            for (int i = 0; i < PopSize; i++)
            {
                eval[i] = RanGen.Next(100);
            }

            GenAlgo.NewGeneration(eval);
        }
    }
}
